<template>
	<view :style="colorStyle" @touchmove.stop.prevent=''>
		<view class="product-window"
			:class="(attr.cartAttr === true ? 'on' : '') + ' ' + (iSbnt?'join':'') + ' ' + (iScart?'joinCart':'')">
			<view class="textpic acea-row row-between-wrapper" @touchmove.stop.prevent="moveHandle">
				<view class="pictrue" @click="showImg()">
					<image :src="attr.productSelect.image"></image>
				</view>
				<view class="text">
					<view class="line2 store-name">
						{{ attr.productSelect.store_name }}
					</view>
					<view class="money font-color" v-if="storeInfo.warehouse_type&&storeInfo.warehouse_type==2">
						<text class="stock"
							v-if='isShow && !type'>{{$t(`库存`)}}{{ attr.productSelect.stock + unitName }}</text>
						<text class='stock'
							v-if="limitNum && type">{{$t(`库存`) }}{{attr.productSelect.quota + unitName}}</text>
						<text class="stock" v-if='minQty > 1 && is_virtual'>{{$t(`起购`)}} {{ minQty + unitName }}</text>
					</view>
					<view class="money font-color">
						<view class="row-middle">
							<view class="price_item">
								<!-- 普通商品的实时金价作为价格展示 -->
								<view v-if="storeInfo.current_gold_price&&storeInfo.metal_type!=4">实时金价{{$t(`￥`)}}<text>{{ currentGoldPrice||0 }}</text></view>
								<!-- 固定商品的工费作为价格展示 -->
								<view v-if="storeInfo.metal_type==4">{{$t(`￥`)}}<text>{{ labor_charges||0 }}</text></view>
							</view>
							<!-- storeInfo.metal_type!=4固定价格商品不显示工费 -->
							<view class="price_item"
								v-if="storeInfo.warehouse_type&&storeInfo.warehouse_type==2&&Number(labor_charges)&&storeInfo.metal_type!=4">
								工费{{$t(`￥`)}}<text>{{labor_charges||0 }}</text>/g
							</view>
							<view class="progress_box" v-if="storeInfo.warehouse_type&&storeInfo.warehouse_type==1">
								<progress :border-radius="8" :percent="(storeInfo.wms.leave/storeInfo.wms.all)*100"
									activeColor="#D29E41" stroke-width="4" />
								<view class="text_box">
									剩余库存：{{storeInfo.wms.leave||0}}克
								</view>
							</view>
							<text class='vip-money'
								v-if="is_vip>0 && attr.productSelect.vip_price">{{$t(`￥`)}}{{attr.productSelect.vip_price}}</text>
							<view class="vipImg" v-if="is_vip>0 && attr.productSelect.vip_price">
								<image src="../../static/images/svip.gif"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="guanbi_box flex_c" @click="closeAttr">
					<view class="iconfont icon-guanbi"></view>
				</view>
			</view>
			<view class="rollTop">
				<scroll-view scroll-y="true" style="height: 100%;">
					<view class="productWinList">
						<view class="item" v-for="(item, indexw) in attr.productAttr" :key="indexw">
							<view class="title">{{ $t(item.attr_name) }}</view>
							<view class="listn acea-row row-middle">
								<view class="itemn" :class="item.index === itemn.attr ? 'on' : ''"
									v-for="(itemn, indexn) in item.attr_value" @click="tapAttr(indexw, indexn)"
									:key="indexn">
									<text v-if="item.attr_name=='克重'">{{ $t(itemn.attr) }}克</text>
									<text v-else>{{ $t(itemn.attr) }}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="cart acea-row row-between-wrapper" v-if="!is_virtual">
						<view class="title">{{$t(`数量`)}}</view>
						<view class="carnum acea-row row-left">
							<text class='stock' v-if="limitNum && !type">{{$t(`限购`)}}{{limitNum + unitName}}</text>
							<text class='stock line' v-if='limitNum && !type && minQty > 1'> | </text>
							<text class="stock" v-if='minQty > 1'>{{$t(`起购`)}}{{ minQty + unitName }}</text>
							<view class="item mysicle reduce acea-row row-center-wrapper"
								:class="attr.productSelect.cart_num <= minQty ? 'on' : ''"
								v-if="attr.productSelect.cart_num <= minQty">
								<text class="iconfont icon-shangpinshuliang-jian"></text>
							</view>
							<view class="item mysicle reduce acea-row row-center-wrapper"
								:class="attr.productSelect.cart_num <= minQty ? 'on' : ''" @click="CartNumDes" v-else>
								<text class="iconfont icon-shangpinshuliang-jian"></text>
							</view>
							<view class='item num acea-row row-middle'>
								<input type="number" v-model="attr.productSelect.cart_num"
									data-name="productSelect.cart_num"
									@input="bindCode(attr.productSelect.cart_num)"></input>
							</view>
							<view v-if="iSplus" class="item mysicle plus acea-row row-center-wrapper" :class="
					      attr.productSelect.cart_num >= attr.productSelect.stock || (limitNum && attr.productSelect.cart_num >= limitNum)
					        ? 'on'
					        : ''
					    " @click="CartNumAdd">
								<text class="iconfont icon-shangpinshuliang-jia"></text>
							</view>
							<view v-else class='item mysicle plus'
								:class='(attr.productSelect.cart_num >= attr.productSelect.quota) || (attr.productSelect.cart_num >= attr.productSelect.product_stock) || (attr.productSelect.cart_num >= attr.productSelect.num) || (type=="seckill" && attr.productSelect.cart_num >= attr.productSelect.once_num) ? "on":""'
								@click='CartNumAdd'>+</view>
						</view>
					</view>
					<view class="cart acea-row row-between-wrapper">
						<view class="title">{{$t(`价格`)}}</view>
						<view class="price_box red">
							<text>￥{{allPrice||0}}</text>
						</view>
					</view>
					<view class="cart acea-row row-between-wrapper" style="border-bottom: none;"
						v-if="storeInfo.sheet_warning">
						<view class="tip_text">
							{{storeInfo.sheet_warning}}
						</view>
					</view>
				</scroll-view>
				
			</view>
			<view class="joinBnt bg-color"
				v-if="iSbnt && attr.productSelect.product_stock>0 &&attr.productSelect.quota>0" @click="goCat">
				{{$t(`我要参团`)}}
			</view>
			<view class="joinBnt on"
				v-else-if="(iSbnt && attr.productSelect.quota<=0)||(iSbnt &&attr.productSelect.product_stock<=0)">
				{{$t(`已售罄`)}}
			</view>
			<view class="joinBnt bg-color" v-if="iScart && attr.productSelect.stock" @click="goCat">{{$t(`确定`)}}</view>
			<view class="joinBnt on" v-else-if="iScart && !attr.productSelect.stock">{{$t(`已售罄`)}}</view>
		</view>
		<view class="mask" @touchmove.stop.prevent="moveHandle" :hidden="attr.cartAttr === false" @click="closeAttr">
		</view>
	</view>
</template>

<script>

	import colors from "@/mixins/color";
	export default {
		mixins: [colors],
		props: {
			authenticate_type: {
				type: Number,
				value: 0
			},
			storeInfo: {
				type: Object,
				default: () => {}
			},
			productValue: {
				type: Object,
				default: () => {}
			},
			attr: {
				type: Object,
				default: () => {}
			},
			currentGoldPrice: {
				type: Number,
				value: 0
			},
			limitNum: {
				type: Number,
				value: 0
			},
			minQty: {
				type: Number,
				value: 0
			},
			isShow: {
				type: Number,
				value: 0
			},
			iSbnt: {
				type: Number,
				value: 0
			},
			iSplus: {
				type: Number,
				value: 0
			},
			iScart: {
				type: Number,
				value: 0
			},
			is_vip: {
				type: Number,
				value: 0
			},
			is_virtual: {
				type: Number,
				value: 0
			},
			type: {
				type: String,
				default: ''
			},
			unitName: {
				type: String,
				default: ''
			},
		},
		created() {},
		computed: {
			allPrice() {
				
				let price=0
				if(this.storeInfo.metal_type!=4){
					// 最终的价格=（实时金价+工费）*克重*数量
					price = (Number(this.currentGoldPrice) + Number(this.labor_charges)) * this.goldWeight * this.attr.productSelect.cart_num
				}else{
					//固定价格商品 最终的价格=工费*数量
					price=this.labor_charges * this.attr.productSelect.cart_num
				}
				 
				console.log(this.currentGoldPrice, this.labor_charges, this.goldWeight, this.attr.productSelect.cart_num,price, "打印结果");
				return price.toFixed(2)	
				
			}
		},
		watch: {
			storeInfo: {
				handler(newVal, oldVal) {
					console.log(newVal, "Sdkfjsdkfksdj");
					console.log(this.attr.productAttr, "便利对象");
					// 默认显示工费
					if (newVal.warehouse_type && newVal.warehouse_type == 2) {
						this.attr.productAttr.forEach((item, indexw) => {
							item.attr_value.forEach((itemn, indexn) => {
								console.log(item.index === itemn.attr, "是否相等");
								if (item.index === itemn.attr) {
									console.log("执行了几次");
									this.tapAttr(indexw, indexn)
								}
							})
						})
					}

				},
				deep: true
			}
		},
		data() {
			return {
				labor_charges: 0,
				id: 0,
				goldWeight: 0
			};
		},

		mounted() {
		},
		methods: {

			moveHandle() {},
			getpreviewImage: function() {
				uni.previewImage({
					urls: this.attr.productSelect.image.split(','),
					current: this.attr.productSelect.image
				});
			},
			goCat: function() {
				this.$emit('goCat');
			},
			/**
			 * 购物车手动输入数量
			 * 
			 */
			bindCode: function(e) {
				this.$emit('iptCartNum', e);
			},
			closeAttr: function() {
				this.$emit('myevent');
			},
			CartNumDes: function() {
				this.$emit('ChangeCartNum', false);
			},
			CartNumAdd: function() {
				this.$emit('ChangeCartNum', true);
			},
			tapAttr: function(indexw, indexn) {

				let that = this;
				that.$emit("attrVal", {
					indexw: indexw,
					indexn: indexn
				});
				this.$set(this.attr.productAttr[indexw], 'index', this.attr.productAttr[indexw].attr_values[indexn]);
				let value = that.getCheckedValue().join(",");
				that.$emit("ChangeAttr", value);
				if (this.limitNum == 1) {
					if (this.attr.productSelect.quota > 0) {
						this.attr.productSelect.cart_num = 1
					} else {
						this.attr.productSelect.cart_num = 0
					}
				}
				console.log(value, "jskdjfsdfkdslk");
				if (!value.includes(',')) {
					this.goldWeight = Number(value)
				} else {
					const weight = value.split(',')
					console.log(weight,"选择的数据");
					weight.forEach((item)=>{
						console.log(!isNaN(item));
						if(!isNaN(item)){
							console.log("这里面是数字");
							this.goldWeight = Number(item)
						}
					})
					
				}
				if (this.authenticate_type == 0) {
					this.labor_charges = this.productValue[value].labor_charges
				} else {
					this.labor_charges = this.productValue[value].certifi_labor_charges
				}
				

			},
			//获取被选中属性；
			getCheckedValue: function() {
				let productAttr = this.attr.productAttr;
				let value = [];
				for (let i = 0; i < productAttr.length; i++) {
					for (let j = 0; j < productAttr[i].attr_values.length; j++) {
						if (productAttr[i].index === productAttr[i].attr_values[j]) {
							value.push(productAttr[i].attr_values[j]);
						}
					}
				}
				return value;
			},
			showImg() {
				this.$emit('getImg');
			},
		}
	}
</script>

<style scoped lang="scss">
	.progress_box {
		font-size: 26rpx;
		color: #B5B5B5;
		margin-top: 50rpx;

		.text_box {
			text-align: right;
			margin-top: 10rpx;
		}
	}

	.vip-money {
		color: #282828;
		font-size: 28rpx;
		font-weight: 700;
		margin-left: 6rpx;
	}

	.vipImg {
		width: 68rpx;
		height: 27rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.red {
		color: #E63523 !important;
	}

	.product-window {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		background-color: #fff;
		z-index: 100;
		border-radius: 16rpx 16rpx 0 0;
		transform: translate3d(0, 100%, 0);
		transition: all .3s cubic-bezier(.25, .5, .5, .9);
		padding-bottom: 140rpx;
		padding-bottom: calc(140rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/

		.store-name {
			font-size: 34rpx;
			color: #2E2E2E;
			font-weight: 700;
		}
	}

	.product-window.on {
		transform: translate3d(0, 0, 0);
	}

	.product-window.join {
		padding-bottom: 30rpx;
	}

	.product-window.joinCart {
		padding-bottom: 30rpx;
		z-index: 10000;
	}

	.product-window .textpic {
		padding: 0 80rpx 0 30rpx;
		margin-top: 29rpx;
		position: relative;
	}

	.product-window .textpic .pictrue {
		width: 150rpx;
		height: 150rpx;
	}

	.product-window .textpic .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.product-window .textpic .text {
		width: 470rpx;
		font-size: 28rpx;
		color: #202020;
	}

	.product-window .textpic .text .money {
		font-size: 28rpx;
		margin-top: 10rpx;
		color: #E63523 !important;
		// .price_item{
		// 	margin-top: 10rpx;
		// }
	}

	.product-window .textpic .text .money .num {
		font-size: 36rpx;
	}

	.product-window .textpic .text .money .stock {
		color: #999;
		margin-left: 6rpx;
		margin-right: 20rpx;
	}

	.product-window .textpic .guanbi_box {
		width: 55rpx;
		height: 55rpx;
		border-radius: 50%;
		overflow: hidden;
		background: #CCCCCC;
		position: absolute;
		right: 30rpx;
		top: -5rpx;

		.iconfont {
			font-size: 35rpx;
			color: #474747;
		}
	}

	.product-window .rollTop {
		height: 500rpx;
		overflow: auto;
		margin-top: 36rpx;
		padding: 0 30rpx;
	}

	.product-window .productWinList {
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #E6E6E6;
	}

	.product-window .productWinList .item~.item {
		margin-top: 36rpx;
	}

	.product-window .productWinList .item .title {
		font-size: 30rpx;
		color: #999;
		// padding: 0 30rpx;
	}

	.product-window .productWinList .item .listn {
		// padding: 0 30rpx 0 16rpx;
	}

	.product-window .productWinList .item .listn .itemn {
		border: 1px solid #F2F2F2;
		font-size: 26rpx;
		color: #282828;
		padding: 7rpx 33rpx;
		border-radius: 25rpx;
		margin: 20rpx 0 0 14rpx;
		background-color: #F2F2F2;
	}

	.product-window .productWinList .item .listn .itemn.on {
		color: var(--view-theme);
		background: var(--view-minorColorT);
		border-color: var(--view-theme);
	}

	.product-window .productWinList .item .listn .itemn.limit {
		color: #999;
		text-decoration: line-through;
	}

	.product-window .cart {
		// margin-top: 36rpx;
		padding: 30rpx 0rpx;
		align-items: center;
		border-bottom: 1rpx solid #E6E6E6;

		.price_box {
			font-size: 34rpx;
		}

		.tip_text {
			font-size: 26rpx;
			color: #FF1717;
		}
	}

	.product-window .cart .title {
		font-size: 30rpx;
		color: #999;
	}

	.product-window .cart .carnum {
		height: 54rpx;
		align-items: center;

		.stock {
			font-size: 20rpx;
			line-height: 54rpx;
			color: #aaa;
		}

		.line {
			padding: 0 6rpx;
		}
	}

	.product-window .cart .carnum .iconfont {
		font-size: 25rpx;
	}

	.product-window .cart .carnum view {
		// border: 1px solid #a4a4a4;
		width: 40rpx;
		height: 40rpx;
		text-align: center;
		color: #282828;
		font-size: 45rpx;
	}

	.product-window .cart .carnum .reduce {
		border-right: 0;
		border-radius: 6rpx 0 0 6rpx;
		font-size: 60rpx;

	}

	.product-window .cart .carnum .mysicle {
		border-radius: 50% !important;
		border: 2rpx solid var(--view-theme);
		overflow: hidden;

		.iconfont {
			color: var(--view-theme);
		}

	}

	.product-window .cart .carnum .reduce.on {
		// border-color: #e3e3e3;
		border: 2rpx solid #e3e3e3;
		color: #DEDEDE !important;

		.iconfont {
			color: #DEDEDE !important;
		}
	}

	.product-window .cart .carnum .plus {
		border-radius: 0 6rpx 6rpx 0;
		// line-height: 46rpx;
	}

	.product-window .cart .carnum .plus.on {
		// border-color: #e3e3e3;
		border: 2rpx solid #e3e3e3;
		color: #dedede !important;

		.iconfont {
			color: #DEDEDE !important;
		}
	}

	.product-window .cart .carnum .num {
		background: rgba(242, 242, 242, 1);
		color: #282828;
		font-size: 28rpx;
		margin: 0 20rpx;
		width: 90rpx;
		border-radius: 4rpx;
		height: 52rpx;
		line-height: 52rpx;
	}

	.product-window .joinBnt {
		font-size: 30rpx;
		width: 620rpx;
		height: 86rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 86rpx;
		color: #fff;
		margin: 21rpx auto 0 auto;
	}

	.product-window .joinBnt.on {
		background-color: #bbb;
		color: #fff;
	}
</style>